import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)
import { getToken } from "@/utils/token.js"

import constantRoutes from "./modules/constantRoutes"

const router = new VueRouter({
  mode: "hash", // hash,history
  base: process.env.BASE_URL,
  routes: constantRoutes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    // 判断该路由是否需要登录权限
    let token = to.query.token
    if (getToken() || token) {
      //localStorage.getItem(‘userMsg‘)是登录后本地保存登录的信息，只有已登录才有
      next()
    } else {
      window.location.href = process.env.VUE_APP_LOGIN_URL
      // next({
      //   path: "/login",
      //   query: {
      //     redirect: to.fullPath
      //   } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      // })
    }
  } else {
    next()
  }
})

export default router
